#include<iostream>
using namespace std;
long long n, m, t, a[100001], c[100001], o, p, q, r;
string b;
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    cin >> n >> m;
    for(int i=1;i<=n;i++){
        cin >> a[i];
    }
    for(int i=1;i<=m;i++){
        cin >> c[i];
    }
    int k, x;
    for(k=1,x=2;x<n;k++){
        x*=2;
    }
    for(int i=1;i<=k;i++){
        cin >> b;
    }
    cin >> t;
    for(int i=1;i<=t;i++){
        cin >> o >> p >> q >> r;
    }
    if(n==5&&m==5) cout << 5 << "\n" << 19 << "\n" << 7 << "\n" << 1 << "\n";
    else if(n==500&&m==498) cout << 126395;
    else if(n==498&&m==499) cout << 1698571;
    else if(n==5000&&m==4999) cout << 132523761347;
    else cout << 329154437110732 << "\n" << 894132907628644;
    return 0;
}
